package com.huxuesong.prcc.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huxuesong.prcc.annotation.SysLog;
import com.huxuesong.prcc.sys.entity.SysWebLog;
import com.huxuesong.prcc.sys.service.ISysWebLogService;
import com.huxuesong.prcc.util.CommonResult;
import com.huxuesong.prcc.util.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author hxs
 * @since 2019-08-02
 */
@Slf4j
@RestController
@Api(tags = "SysWebLogController", description = "管理")
@RequestMapping("/sys/SysWebLog")
public class SysWebLogController {
@Resource
private ISysWebLogService ISysWebLogService;

@SysLog(MODULE = "sys", REMARK = "根据条件查询所有列表")
@ApiOperation("根据条件查询所有列表")
@GetMapping(value = "/list")
@PreAuthorize("hasAuthority('sys:SysWebLog:read')")
public Object getSysWebLogByPage(SysWebLog entity,
@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize
        ){
        try{
        return new CommonResult().success(ISysWebLogService.page(new Page<SysWebLog>(pageNum,pageSize),new QueryWrapper<>(entity)));
        }catch(Exception e){
        log.error("根据条件查询所有列表：%s",e.getMessage(),e);
        }
        return new CommonResult().failed();
        }
@SysLog(MODULE = "sys", REMARK = "保存")
@ApiOperation("保存")
@PostMapping(value = "/create")
@PreAuthorize("hasAuthority('sys:SysWebLog:create')")
public Object saveSysWebLog(@RequestBody SysWebLog entity){
        try{
        if(ISysWebLogService.save(entity)){
        return new CommonResult().success();
        }
        }catch(Exception e){
        log.error("保存：%s",e.getMessage(),e);
        return new CommonResult().failed();
        }
        return new CommonResult().failed();
        }

@SysLog(MODULE = "sys", REMARK = "更新")
@ApiOperation("更新")
@PostMapping(value = "/update/{id}")
@PreAuthorize("hasAuthority('sys:SysWebLog:update')")
public Object updateSysWebLog(@RequestBody SysWebLog entity){
        try{
        if(ISysWebLogService.updateById(entity)){
        return new CommonResult().success();
        }
        }catch(Exception e){
        log.error("更新：%s",e.getMessage(),e);
        return new CommonResult().failed();
        }
        return new CommonResult().failed();
        }
@SysLog(MODULE = "sys", REMARK = "删除")
@ApiOperation("删除")
@GetMapping(value = "/delete/{id}")
@PreAuthorize("hasAuthority('sys:SysWebLog:delete')")
public Object deleteSysWebLog(@ApiParam("id") @PathVariable Long id){
        try{
        if(ValidatorUtils.empty(id)){
        return new CommonResult().paramFailed("id");
        }
        if(ISysWebLogService.removeById(id)){
        return new CommonResult().success();
        }
        }catch(Exception e){
        log.error("删除：%s",e.getMessage(),e);
        return new CommonResult().failed();
        }
        return new CommonResult().failed();
        }
@SysLog(MODULE = "sys", REMARK = "给分配")
@ApiOperation("查询明细")
@GetMapping(value = "/{id}")
@PreAuthorize("hasAuthority('sys:SysWebLog:read')")
public Object getSysWebLogById(@ApiParam("id") @PathVariable Long id){
        try{
        if(ValidatorUtils.empty(id)){
        return new CommonResult().paramFailed("id");
        }
    SysWebLog coupon= ISysWebLogService.getById(id);
        return new CommonResult().success(coupon);
        }catch(Exception e){
        log.error("查询明细：%s",e.getMessage(),e);
        return new CommonResult().failed();
        }

        }
@ApiOperation(value = "批量删除")
@RequestMapping(value = "/delete/batch", method = RequestMethod.GET)
@ResponseBody
@SysLog(MODULE = "pms", REMARK = "批量删除")
@PreAuthorize("hasAuthority('sys:SysWebLog:delete')")
public Object deleteBatch(@RequestParam("ids") List<Long> ids){
        boolean count= ISysWebLogService.removeByIds(ids);
        if(count){
        return new CommonResult().success(count);
        }else{
        return new CommonResult().failed();
        }
        }

        }
